package com.qiall.game.gobang;

import lombok.Data;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;


public class RoomPool {

    private static Map<Integer, Room> sessionId1RoomMap = new ConcurrentHashMap<>();
    public static Map<String, Integer> sessionStatusMap = new ConcurrentHashMap<>();
    private static AtomicInteger atomicInteger = new AtomicInteger(0);
    private static Room waitRoom = new Room(atomicInteger.incrementAndGet());

    @Data
    public static class Room {
        public Room(Integer roomId) {
            this.roomId = roomId;

        }

        public void initBoard() {
            this.checkerboard=new ArrayList<>();
        }

        private Integer roomId;
        private String sessionId1;
        private String sessionId2;
        /**
         * 当前可以落子的sessionId
         */
        private String currentMoveSession;
        private List<Point> checkerboard=new ArrayList<>();
        public static byte blank = 0;
        public static byte sessionId1L = 1; // sessionId1
        public static byte sessionId2L = 2; //sessionId2

        /**
         * 1-等待
         * 2-开始对局
         * 3-对局结束
         */
        public static Integer wait = 1;
        public static Integer enter = 2;
        public static Integer end = 3;
        private Integer roomStatus;

        @Data
        public static class Point {
            private int x;
            private int y;
            byte sessionId;
        }
    }

    /**
     * 新增等待房间
     */
    public static void addWaitRoom() {
        sessionId1RoomMap.put(waitRoom.getRoomId(), waitRoom);
        waitRoom = new Room(atomicInteger.incrementAndGet());
        waitRoom.setRoomStatus(Room.wait);
    }

    public static Integer enterRoom(String sessionId) {

        Integer roomId = waitRoom.roomId;
        if (waitRoom.getSessionId1() == null) {
            sessionStatusMap.put(sessionId, Room.wait);
            waitRoom.setSessionId1(sessionId);
            waitRoom.setCurrentMoveSession(sessionId);
        } else {
            if (sessionId.equals(waitRoom.getSessionId1())) {
                return roomId;
            }
            waitRoom.setRoomStatus(Room.enter);
            waitRoom.setSessionId2(sessionId);
            sessionStatusMap.put(waitRoom.getSessionId1(), Room.enter);
            sessionStatusMap.put(waitRoom.getSessionId2(), Room.enter);
            addWaitRoom();
            TimerTask timerTask = new TimerTask() {
                @Override
                public void run() {
                    sessionId1RoomMap.remove(waitRoom.getRoomId());
                    sessionStatusMap.remove(waitRoom.getSessionId1());
                    sessionStatusMap.remove(waitRoom.getSessionId2());
                }
            };
            // 延时六小时执行 todo 通过配置设置超时session 丢弃时间
            Timer timer = new Timer();
            timer.schedule(timerTask, 1000 * 60 * 60 * 6);
        }
        return roomId;
    }

    public static Room getWaitRoom() {
        return waitRoom;
    }

    public static Room getRoom(Integer roomId) {
        return sessionId1RoomMap.get(roomId);
    }

    public static Integer getSessionStatus(String sessionId) {
        return sessionStatusMap.get(sessionId);
    }
}
